package phase1.part1;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 
 * 
 * @author John Girata
 * @version Nov 8, 2012
 */
public class State {
	private String label;
	private boolean accepting;
	private String tokenClass;
	private List<Transition> transitions;

	private static StateLabeler labeler;

	static {
		labeler = new StateLabeler();
	}

	public State(String label, boolean accepting) {
		this.label = label;
		this.accepting = accepting;

		tokenClass = "";
		transitions = new ArrayList<>();
	}

	public State(boolean accepting) {
		this(labeler.getNextStateName(), accepting);
	}

	public State() {
		this(false);
	}

	public State(String label) {
		this(label, false);
	}

	public State(State state) {
		label = labeler.getNextStateName();
		accepting = state.accepting;
		tokenClass = state.tokenClass;
		transitions = new ArrayList<>();

		for (Transition t : state.transitions) {
			transitions.add(new Transition(t));
		}
	}

	public boolean isAccepting() {
		return accepting;
	}

	public void setAccepting(boolean accepting) {
		this.accepting = accepting;
	}

	public String getTokenClass() {
		return tokenClass;
	}

	public void setTokenClass(String tokenClass) {
		this.tokenClass = tokenClass;
	}

	public String getLabel() {
		return label;
	}

	public void addTransition(Transition transition) {
		transitions.add(transition);
	}

	/**
	 * Creates a new epsilon transition to end
	 * 
	 * @param end
	 *            The state this transition points to
	 */
	public void addTransition(State end) {
		addTransition(new Transition(end));
	}

	/**
	 * Creates a new transition to end with the given pattern
	 * 
	 * @param end
	 *            The state this transition points to
	 * @param pattern
	 *            This transition's pattern
	 */
	public void addTransition(State end, String pattern) {
		addTransition(new Transition(end, pattern));
	}

	public List<Transition> getTransitions() {
		return transitions;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((label == null) ? 0 : label.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof State))
			return false;
		State other = (State) obj;
		if (label == null) {
			if (other.label != null)
				return false;
		} else if (!label.equals(other.label))
			return false;
		return true;
	}

	@Override
	public String toString() {
		if (transitions.isEmpty()) {
			return label + (isAccepting() ? " (accepting)" : "");
		}

		if (transitions.size() != 1) {
			return "State [label=" + label + ", accepting=" + accepting
					+ ", tokenClass=" + tokenClass + "]";
		}

		Transition t = transitions.get(0);

		return label
				+ " ---"
				+ (t.getPattern() == null ? "epsilon" : "["
						+ t.getPattern().getPattern() + "]") + "---> "
				+ t.getEnd();
	}
	
	public void print(PrintStream stream) {
		print(this, stream);
	}

	public static void print(State state, PrintStream stream) {
		print(state, stream, new HashSet<State>());
	}

	public static void print(State state, PrintStream stream, Set<State> visited) {
		if (state == null) {
			return;
		}

		if (visited.contains(state)) {
			return;
		}

		visited.add(state);

		for (Transition t : state.getTransitions()) {
			stream.print(state.getLabel() + ",");

			if (!t.isEpsilonTransition()) {
				stream.print(t.getPattern().getCharacters() + ",");
			}

			stream.println(t.getEnd().getLabel());
			print(t.getEnd(), stream, visited);
		}
	}

	private static class StateLabeler {
		private int counter;

		public String getNextStateName() {
			return "" + counter++;
		}
	}
}
